//
//  ChatViewController.m
//  PigMarket
//
//  Created by mac on 2019/6/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//



#import "ChatViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "ChatSetUpViewController.h"
#import "GroupSetUpViewController.h"
#import <TUIInputBar.h>
@interface ChatViewController ()<TUIChatControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIDocumentPickerDelegate>
/**
 chat
 */
@property (nonatomic , strong) TUIChatController *chat;
@end

@implementation ChatViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpView];
    [self setUpNav];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpNav {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"discountcoupon_more"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(moreClick)];

}
- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    TIMConversation *conv = [[TIMManager sharedInstance] getConversation:self.conversation.convType receiver:self.conversation.convId];
    
    self.chat = [[TUIChatController alloc] initWithConversation:conv];
    self.chat.delegate = self;
    [self addChildViewController:self.chat];
    [self.view addSubview:self.chat.view];
    self.title = self.conversation.title;
}

- (void) initLayout {
    
}

- (void) initAction {
    UIButton *recordButton = self.chat.inputController.inputBar.recordButton;
    [recordButton addTarget:self action:@selector(recordButtonClick:) forControlEvents:UIControlEventTouchDown];
}

- (void)initData {
    
}

#pragma mark - delegate
//发送相册 拍照 视频  文件 权限校验
- (void)chatController:(TUIChatController *)chatController onSelectMoreCell:(TUIInputMoreCell *)cell {
    if (cell.data == [TUIInputMoreCellData photoData]) {
        if ([[AuthorizationManager sharedManager] camerAuthorizationAllowed]) {}
    }
    if (cell.data == [TUIInputMoreCellData videoData]) {
        if ([[AuthorizationManager sharedManager] microphoneAuthorizationAllowed]) {}

    }
    if (cell.data == [TUIInputMoreCellData pictureData]) {
        if ([[AuthorizationManager sharedManager] photoAlbumAuthorizationAllowed]) {}
    }
}

#pragma mark - target action
- (void)moreClick {
    switch (self.conversation.convType) {
        case TIM_C2C:
        {
             [[TIMFriendshipManager sharedInstance] getFriendList:^(NSArray<TIMFriend *> *friends) {
                for (TIMFriend *friend in friends) {
                    if ([friend.identifier isEqualToString:self.conversation.convId]) {
                        ChatSetUpViewController *vc = [[ChatSetUpViewController alloc] init];
                        vc.friendProfile = friend;
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                }
            } fail:^(int code, NSString *msg) {
                [self toastWithContent:msg];
            }];
         }
            break;
        case TIM_GROUP:
        {
            GroupSetUpViewController *vc = [[GroupSetUpViewController alloc] init];
            vc.groupId = self.conversation.convId;
            vc.groupName = self.conversation.title;
            [self.navigationController pushViewController:vc animated:YES];

        }
            break;
            
        default:
            break;
    }
}
- (void)recordButtonClick:(UIButton *)button {
    //查看是否开启麦克风权限
    if ([[AuthorizationManager sharedManager] microphoneAuthorizationAllowed]) {
        
    }
}
#pragma mark - private method

#pragma mark - getter and setter




@end
